<?php

include_once 'model/Session.php';
include_once 'URI.php';

class Router {

	//private static $model;	
	//private static $view;
	//private static $controller;
	
	const DISPLAY_ALL = 1;
	const DISPLAY_OWN = 2;
	const DISPLAY_AUTOR = 3;
	
	
	private static function getParam($name) { //tak w razie jakby trzeba bylo cos zmienic w sposobie przekazywania parametr�w.
		if(isset($_GET[$name]))
			return $_GET[$name];
		return null;
	}

	private static function getObject($objectType) {
		$param = self::getParam('controller');
		$objectType = strtolower($objectType);
		$className = ucwords(strtolower($param ? $param : "main")).ucwords($objectType); //case insensitive
		$path = $objectType.'/'.$className.'.php';
		if(!file_exists($path)) {
			echo $path;
			return null;
		}
		include_once $path;
		return $className;
	}
		
	public static function getModel() {
		return self::getObject('Model');	
	}

	public static function getController() {
		return self::getObject('Controller');			
	} 
	
	public static function getAction() {		
		$act = self::getParam("action");
		return $act ? $act : 'index';
	}
	
	public static function getView() {	
		return self::getObject('View');	
	} 	
	
	private static function parse($tab) {
		if(!$tab)
			return null;
		$keys = array_keys($tab);
		foreach($keys as &$key) {
			$k = $key;//strtolower($key);
			$send->$k = $tab[$key]; //TODO: dodać hack-proof?
		}
		return $send;
	}
	
	public static function getParams() {
		$params->loggedUserName = Session::getLoggedUserName();
		$params->loggedUserID = Session::getLoggedUserID();
		$params->pageOwner = self::getParam('user');
		$params->action = self::getParam('action');
		$params->postParams = self::parse($_POST);
		$params->files = self::parse($_FILES);
		$params->begin = self::getParam('begin');
		if($params->begin == null)
			$params->begin = -1;
		$display = self::getParam('display');
		switch($display) {
			case 'all':
				$params->display = DISPLAY_ALL;
				break;
			case 'own':
				$params->display = DISPLAY_OWN;
				break;
			case 'autor':
				$params->display = DISPLAY_AUTOR;
				break;
		}
		return $params;
	}

}

?>